unit UntFormCheckSelecao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, AppEvnts, StdCtrls, Buttons, ExtCtrls,
  cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore,
  dxSkinsDefaultPainters, cxContainer, cxCheckListBox, CheckLst, cxButtons, UntBaseCadastro;

type
  TFrmFormCheckSelecao = class(TFrmBaseCadastro)
    clbSelecao: TCheckListBox;
    clbSelecaoId: TCheckListBox;
    procedure FormShow(Sender: TObject);
    procedure btnTodosClick(Sender: TObject);
    procedure btnNenhumClick(Sender: TObject);
    procedure btnConfirmarClick(Sender: TObject);
    procedure btnSalvarClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
    vTabela, vCampoId, vCampoListagem, vFiltro, vSelecionado : string;
    vListSelecao : string;
  end;

var
  FrmFormCheckSelecao: TFrmFormCheckSelecao;

implementation
uses untDataModule;
{$R *.dfm}

procedure TFrmFormCheckSelecao.btnConfirmarClick(Sender: TObject);
var
  i : integer;
begin
  vListSelecao := '';
  for I := 0 to clbSelecao.Count - 1 do
  begin
    if clbSelecao.Checked[i] then
    begin
      if vListSelecao <> '' then
        vListSelecao := vListSelecao + ',';
      vListSelecao := vListSelecao + clbSelecaoId.Items[i];
    end;
  end;
  ModalResult := mrOk;
end;

procedure TFrmFormCheckSelecao.btnNenhumClick(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to clbSelecao.Count - 1 do
    clbSelecao.Checked[i] := False;
end;

procedure TFrmFormCheckSelecao.btnSalvarClick(Sender: TObject);
var
  i : integer;
begin
  vListSelecao := '';
  for I := 0 to clbSelecao.Count - 1 do
  begin
    if clbSelecao.Checked[i] then
    begin
      if vListSelecao <> '' then
        vListSelecao := vListSelecao + ',';
      vListSelecao := vListSelecao + clbSelecaoId.Items[i];
    end;
  end;
  ModalResult := mrOk;
end;

procedure TFrmFormCheckSelecao.btnTodosClick(Sender: TObject);
var
  i : integer;
begin
  for i := 0 to clbSelecao.Count - 1 do
    clbSelecao.Checked[i] := True;
end;

procedure TFrmFormCheckSelecao.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    Key := #0;
    Perform(Wm_NextDlgCtl,0,0);
  end;
end;

procedure TFrmFormCheckSelecao.FormShow(Sender: TObject);
var
  vDsCampoListagem : string;
  i : integer;
begin
  DM.qLookup.SQL.Clear;
  DM.qLookup.SQL.Add('SELECT '+vCampoId+', '+vCampoListagem+' FROM '+vTabela+' WHERE 0=0 '+vFiltro+' ORDER BY 2, 1');
  DM.qLookup.Close;
  DM.qLookup.Open;
  i := 0;
  while not DM.qLookup.Eof do
  begin
    vDsCampoListagem := DM.qLookup.Fields[1].AsString;
    clbSelecao.Items.Add(vDsCampoListagem);
    clbSelecaoId.Items.Add(DM.qLookup.Fields[0].AsString);

    if (vSelecionado <> '') and (Pos(','+DM.qLookup.Fields[0].AsString+',', ','+vSelecionado+',') > 0) then
      clbSelecao.Checked[i] := True;

    DM.qLookup.Next;
    i := i + 1;
  end;
end;
end.

